require 'rake/clean'

task :default => 'tutorial:test'

desc '"rake run" runs the tutorial as a desktop app'
task :run => 'tutorial:run'

cwd = File.dirname(__FILE__)

require 'rbconfig'
irbin = RbConfig::CONFIG['bindir']
ir = "#{irbin}/ir.exe"
raise "Could not find ir.exe" unless File.exist? ir
IR = ir

# TODO: Mono support -- (jimmysch) Moonlight only as Mono doesn't support WPF
CSC="#{ENV['SystemRoot']}\\Microsoft.NET\\Framework\\v3.5\\csc.exe"
CSC2="#{ENV['SystemRoot']}\\Microsoft.NET\\Framework\\v2.0.50727\\csc.exe"

if ENV["DLR_ROOT"]
  root = ENV['DLR_ROOT']
  # This is a dev environment. See http://wiki.github.com/ironruby/ironruby
  BIN = "#{root}/Languages/Ruby/Scripts/bin"
  RUBY_STDLIBS = "#{root}/Languages/Ruby/StdLib/ruby/1.9.1"
  IRONRUBY_LIBS = "#{root}/Languages/Ruby/Libs"
  GEMS = "#{root}/External.LCA_RESTRICTED/Languages/Ruby/ruby-1.8.6p368/lib/ruby/gems/1.8/gems"

  chiron = "#{root}/Bin/Debug/Chiron.exe"
  chiron = "#{root}/Bin/Release/Chiron.exe" unless File.exist?(chiron)
  raise "Could not find Chiron.exe. Do you have a build of Silverlight? If not, type 'bsd'" unless File.exist?(chiron)
  CHIRON = chiron
else
  BIN = irbin
  RUBY_STDLIBS = "#{irbin}/../lib/ruby/1.8"
  IRONRUBY_LIBS = RUBY_STDLIBS
  GEMS = "#{irbin}/../lib/IronRuby/gems/1.8/gems"
  CHIRON = "#{irbin}/../silverlight/bin/Chiron.exe"

  if File.exist?("#{GEMS}/minitest-1.4.2")
    `#{BIN}/igem install minitest --version 1.4.2 --no-rdoc --no-ri`
  end
end

CLEAN.include("app.xap", "deploy","app/Libs","app/Tutorials/*_tutorial.generated.html", "dlr.js")
CLOBBER.include("**/SplashScreen.dll","**/SplashScreen.g.resources","**/TutorialSamples.dll")

namespace 'tutorial' do
  
  desc 'Copy any required Ruby libraries to a subfolder for use in the tutorial'
  task :build => [:clean, "wpf/Splashscreen.dll","app/Tutorials/ironruby_files/TutorialSamples.dll"] do
    cd cwd do
      mkdir 'app/Libs' unless File.exist?('app/Libs')
      cp "#{RUBY_STDLIBS}/erb.rb", "app/Libs"
      %w(stringio.rb bigdecimal.rb).each{|i| cp "#{IRONRUBY_LIBS}/#{i}", 'app/Libs' }
      cp_r "#{RUBY_STDLIBS}/rdoc", "app/Libs"
      cp_r "#{GEMS}/minitest-1.4.2", "app/Libs"
      cp "#{File.dirname(CHIRON)}/dlr.js", '.'
    end
  end

  desc 'Preparing tutorial for deploying to ironruby.net/tutorial'
  task :deploy => [:build, :generate_html] do
    cd cwd do
      `#{CHIRON} /s /d:app /z:app.xap`
      rm_r 'deploy' if File.exist? 'deploy'
      mkdir 'deploy'
      %w(app.xap sl_tutorial.html console_tutorial.rb).each{|i| cp i, 'deploy'}
      %w(test app).each{|i| cp_r i, 'deploy'}
      Dir['app/Tutorials/*generated.html'].each{|i| cp i, 'deploy'}
      cp "#{File.dirname(CHIRON)}/dlr.js", 'deploy'
    end
    puts "Deployment finished: see the 'deploy' directory"
  end

  desc 'Test the tutorial'
  task :test => ['test:desktop', 'test:silverlight']

  namespace 'test' do
    desc "Test the tutorial on the desktop"
    task :desktop => :build do
      cd "#{cwd}/test" do
        puts `#{IR} test_console.rb`
      end
    end
    
    desc "Test the tutorial in Silverlight"
    task :silverlight => :build do
      `#{CHIRON} /n /b:sl_tutorial.html?test`
    end
  end

  desc 'Generate HTML for tutorial'
  task :generate_html => :clean do
    cd(cwd){ `#{IR} html_tutorial.rb` }
  end

  desc 'Run the tutorial (desktop is default)'
  task :run => :desktop
  
  file "wpf/Splashscreen.dll" => "wpf/SplashScreen.cs" do
    cd "#{cwd}/wpf" do
       system "#{CSC} /t:library /r:WindowsBase.dll SplashScreen.cs"
       system "#{IR} -e \"require 'SplashScreen.dll'; SplashScreen::SplashScreen.write_image_resource 'SplashScreen.png', 'SplashScreen.g.resources'\""
       system "#{CSC} /t:library /r:WindowsBase.dll /resource:SplashScreen.g.resources SplashScreen.cs"
    end
  end

  file "app/Tutorials/ironruby_files/TutorialSamples.dll" => "app/Tutorials/ironruby_files/Prime.cs" do
    cd "#{cwd}/app/Tutorials/ironruby_files" do
       system "#{CSC2} /t:library Prime.cs"
       mv "Prime.dll","TutorialSamples.dll"
    end
  end
  
  desc 'Run the tutorial as a desktop application'
  task :desktop  => ["wpf/Splashscreen.dll","app/Tutorials/ironruby_files/TutorialSamples.dll"] do
     cd(cwd){ `#{IR} wpf_tutorial.rb` }
  end

  desc 'Run the tutorial in the web browser'
  task :silverlight => :build do
     cd(cwd){ `#{CHIRON} /n /b:sl_tutorial.html` }
  end

end
